Page({
  data: {
    userInfo: {
      avatarUrl: '',
      nickname: '',
      userId: '',
      active: 0,
      activePercent: 0
    },
    quickActions: [
      { name: '我的订单', icon: 'order.png' },
      { name: '维修报修', icon: 'repair.png' },
      { name: '实名认证', icon: 'auth.png' },
      { name: '我的钱包', icon: 'wallet.png' },
      { name: '联系客服', icon: 'service.png' }
    ],
    listSections: [
      {
        title: '账号与安全',
        items: [
          { name: '账号', icon: 'account.png' },
          { name: '我的收藏', icon: 'fav.png' },
          { name: '实名认证', icon: 'auth.png' }
        ]
      },
      {
        title: '其他',
        items: [
          { name: '我的钱包', icon: 'wallet.png' },
          { name: '浏览记录', icon: 'history.png' },
          { name: '设置', icon: 'setting.png' }
        ]
      }
    ]
  },
  onLoad() {
    // 获取用户信息
    const userInfo = wx.getStorageSync('userInfo') || {};
    this.setData({
      userInfo: {
        avatarUrl: userInfo.avatarUrl || '',
        nickname: userInfo.nickname || '',
        userId: userInfo.userId || '',
        active: userInfo.active || 0,
        activePercent: userInfo.activePercent || 0
      }
    });
  },
  onShow() {
    // 重新获取用户信息，保证登录后能立即显示
    const userInfo = wx.getStorageSync('userInfo') || {};
    this.setData({
      userInfo: {
        avatarUrl: userInfo.avatarUrl || '',
        nickname: userInfo.nickname || '',
        userId: userInfo.userId || '',
        active: userInfo.active || 0,
        activePercent: userInfo.activePercent || 0
      }
    });
    console.log('当前头像路径:', userInfo.avatarUrl);
  },
  onSignIn() {
    const today = new Date().toISOString().slice(0, 10); // yyyy-mm-dd
    const lastSignDate = wx.getStorageSync('lastSignDate');
    if (lastSignDate === today) {
      wx.showToast({
        title: '今日已签到',
        icon: 'none'
      });
      return;
    }
    // 签到成功，增加活跃值
    let userInfo = this.data.userInfo;
    userInfo.active += 10;
    userInfo.activePercent = Math.min(userInfo.active / 100 * 100, 100);
    this.setData({ userInfo });
    wx.setStorageSync('userInfo', userInfo);
    wx.setStorageSync('lastSignDate', today);
    wx.showToast({
      title: '签到成功',
      icon: 'success'
    });
  },
  onQuickAction(e) {
    const index = e.currentTarget.dataset.index;
    const action = this.data.quickActions[index];
    switch (action.name) {
      case '我的订单':
        wx.navigateTo({ url: '/pages/history/history' });
        break;
      case '维修报修':
        wx.navigateTo({ url: '/pages/repair/repair' });
        break;
      case '实名认证':
        wx.navigateTo({ url: '/pages/auth/auth' });
        break;
      case '我的钱包':
        wx.navigateTo({ url: '/pages/wallet/wallet' });
        break;
      case '联系客服':
        wx.makePhoneCall({ phoneNumber: '400-123-4567' });
        break;
    }
  },
  onListItemTap(e) {
    const { section, index } = e.currentTarget.dataset;
    const item = this.data.listSections[section] && this.data.listSections[section].items[index];
    // console.log('点击参数 section:', section, 'index:', index);
    // console.log('当前 listSections:', this.data.listSections);
    // const item = this.data.listSections[section] && this.data.listSections[section].items[index];
    // console.log('item:', item);
    if (!item) {
      wx.showToast({ title: '菜单项不存在', icon: 'none' });
      return;
    }
    switch (item.name) {
      case '账号':
        wx.navigateTo({ url: '/pages/account/account' });
        break;
      case '我的收藏':
        wx.navigateTo({ url: '/pages/favorite/favorite' });
        break;
      case '实名认证':
        wx.navigateTo({ url: '/pages/auth/auth' });
        break;
      case '我的钱包':
        wx.navigateTo({ url: '/pages/wallet/wallet' });
        break;
      case '浏览记录':
        wx.navigateTo({ url: '/pages/history/history' });
        break;
      case '设置':
        wx.navigateTo({ url: '/pages/setting/setting' });
        break;
    }
  },
  onUserInfoTap() {
    if (!this.data.userInfo || !this.data.userInfo.userId) {
      wx.navigateTo({
        url: '/pages/login/login'
      });
    }
  }
}); 